#!/usr/bin/make -f

CFLAGS := $(shell dpkg-buildflags --get CFLAGS)
CXXFLAGS := $(shell dpkg-buildflags --get CXXFLAGS)
LDFLAGS := $(shell dpkg-buildflags --get LDFLAGS) -Wl,--as-needed

DEB_HOST_ARCH  ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

# These don't need to be exported and if they are we'll get the flags
# duplicated in the command line.
unexport CFLAGS
unexport CXXFLAGS
unexport LDFLAGS

ifeq ($(DEB_HOST_ARCH),amd64)
CMAKE_ARCH_FLAGS = -DENABLE_SSE2=ON
else
CMAKE_ARCH_FLAGS = -DENABLE_SSE2=OFF
endif

# setup architecture-specific patches
ifeq ($(DEB_HOST_ARCH),armel)
QUILT_SERIES = series-armel
else
QUILT_SERIES = series-all
endif
export QUILT_SERIES

CMAKE_FLAGS = \
	-DCMAKE_INSTALL_PREFIX=/usr \
	-DCMAKE_VERBOSE_MAKEFILE=ON \
	-DCMAKE_C_FLAGS_RELEASE="$(CFLAGS)" \
	-DCMAKE_CXX_FLAGS_RELEASE="$(CXXFLAGS)" \
	-DCMAKE_SHARED_LINKER_FLAGS_RELEASE="$(LDFLAGS)" \
	-DCMAKE_BUILD_TYPE=Release	\
	-DENABLE_SSE=OFF	\
	-DENABLE_SSE3=OFF	\
	-DENABLE_SSSE3=OFF	\
	-DUSE_FAST_MATH=ON	\
	-DUSE_O3=OFF	\
	-DUSE_OMIT_FRAME_POINTER=ON	\
	-DBUILD_EXAMPLES=ON	\
	-DINSTALL_C_EXAMPLES=ON	\
	-DINSTALL_OCTAVE_EXAMPLES=OFF	\
	-DINSTALL_PYTHON_EXAMPLES=ON	\
	-DBUILD_NEW_PYTHON_SUPPORT=ON	\
	-DBUILD_SWIG_PYTHON_SUPPORT=ON	\
	-DBUILD_OCTAVE_SUPPORT=OFF	\
	-DUSE_IPP=ON	\
	-DWITH_FFMPEG=ON	\
	-DWITH_GSTREAMER=OFF	\
	-DWITH_QT=ON	\
	-DWITH_JPEG=ON	\
	-DWITH_PNG=ON	\
	-DWITH_TIFF=ON	\
	-DWITH_PVAPI=ON	\
	-DWITH_TBB=OFF	\
	-DWITH_UNICAP=OFF	\
	-DWITH_XINE=OFF	\
	-DBUILD_TESTS=ON	\
	-DCMAKE_SKIP_RPATH=ON	\
	-DWITH_JASPER=OFF	\
	-DWITH_OPENEXR=OFF	\
	-DWITH_GTK=OFF	\
	-DWITH_V4L=ON	\
	-DWITH_1394=OFF

%:
	dh $@ --with quilt --buildsystem=cmake

override_dh_clean:
	rm -rf modules/python/src2/hdr_parser.pyc

	dh_clean -O--buildsystem=cmake

override_dh_auto_configure:
	dh_auto_configure -- $(CMAKE_FLAGS) $(CMAKE_ARCH_FLAGS)

override_dh_install:
	-rm --verbose -rf debian/tmp/usr/share/opencv/samples/octave # just in case
	-rm --verbose -f debian/tmp/usr/share/opencv/samples/c/cvsample.dsp
	-rm --verbose -f debian/tmp/usr/share/OpenCV/doc/license.txt
	chmod --verbose +x debian/tmp/usr/share/opencv/samples/c/build_all.sh
	for f in $$(ls obj-*/lib/libopencv_*_s.a); do \
		cp --verbose --preserve=mode $$f debian/tmp/usr/lib/$$(basename $$f _s.a).a; done
	mkdir -p debian/tmp/tests
	for f in $$(ls obj-*/bin/opencv_test_*); do \
		cp --verbose --preserve=mode $$f debian/tmp/tests/$$(basename $$f); done
	dh_install -O--buildsystem=cmake --list-missing

aegis-deb-add -control debian/libopencv-ts2.3/DEBIAN/control debian/libopencv-ts2.3.aegis=_aegis
